'''1、淘宝商品比价定向爬虫
目标：获取淘宝搜索页面的信息，提取其中的商品名称和价格 理解： 淘宝的搜索接口 翻页的处理
技术路线：requests‐bs4‐re
'''
import requests, re, json, csv, time
from requests.exceptions import ReadTimeout, ConnectionError, RequestException



class Taobao(object):
    def __init__(self):
        pass
    def parse_hetml(self, html_doc):
        content = re.findall(r'g_page_config = (.*?) og_srp_ladCss', html_doc, re.S)[0].strip()[:-1]
        obj = json.loads(content)
        list_1 = obj['mods']['grid']['data']['spus']
        for phone in list_1:
            list_num = []
            list_num.append(phone['title'])
            list_num.append(phone['price'])
            with open('Taobao.csv', 'a+', newline='', encoding='utf-8') as f_csv:
                writer = csv.writer(f_csv)
                writer.writerow(list_num)

    def request_rul(self,url_string):
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64)',
            'Host': 's.taobao.com'
        }
        try:
            with requests.get(url_string, headers=header) as f:
                if f.status_code == requests.codes.ok:
                    return f.content.decode('utf-8')
                else:
                    print('status:', f.status_code,f.reason)
        except ReadTimeout:
            print('Time Out')
        except ConnectionError:
            print('Connect Error')
        except RequestException:
            print('Request Error')


    def get_html_data(self, index):
        url_string = r'https://s.taobao.com/search?q=%E6%89%8B%E6%9C%BA&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&p4ppushleft=5%2C48&s='+str((index-1)*48)
        response = self.request_rul(url_string)
        if response:
           self.parse_hetml(response)
        else:
            print('No datas')



    def main(self):
        with open('Taobao.csv', 'a+', newline='', encoding='utf-8') as f_csv:
            writer = csv.writer(f_csv)
            writer.writerow(['手机名称', '手机单价'])
        for index in range(1,10):
            self.get_html_data(index)
            time.sleep(5)


if __name__ == '__main__':
    taobao = Taobao()
    taobao.main()
